#include "stdafx.h"

#if !defined(SOLIDWORKS_HEADERS)
#define SOLIDWORKS_HEADERS
// Solidworks includes
#include <amapp.h>
#include <swpublished_i.h>
//#include <swpublished_i.c>
//#include <sldworks_i.c>
#include <swconst.h>
#endif

#include "SWX.h"

extern CComPtr<ISldWorks> swApp;

namespace swxbatch_ceefit {
	SWX::SWX(void)
	{
	}

	SWX::~SWX(void)
	{
	}

	CComPtr<ISldWorks> SWX::StartSWX() {
		CoInitialize(NULL);

		try {
			if( swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER) != S_OK ) throw 0;
		}
		catch (CException *ex) {
			wchar_t sError[MAX_PATH];
			ex->GetErrorMessage(sError, MAX_PATH);
			swApp = NULL;
			swApp.Release();
			CoUninitialize();
		}
		return swApp;

	}

	void SWX::StopSWX(CComPtr<ISldWorks> swApp) {
		HRESULT res = swApp->ExitApp();
		if (res != S_OK) swApp->ExitApp();
	}
} //namespace swxbatch_ceefit